<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>setter&getter</h1>
    <p>setter和getter用于定义类中变量的设置器和访问器</p>
    <script>
        class Student {

            constructor(name,age) {
                this.name = name;
                this.age = age;
                this.sex = "男";
            }

            get name() {
                console.log("获取name");
                return this._name;
            }

            set name(name) {
                console.log("设置name");
                this._name = name;
            }

            set sex(sex) {
                return this._sex = sex;
            }
        }

        let stu = new Student("奉先",35);
        console.log(stu);
        console.log(stu.name);
        // console.log(stu.age);

        let obj = {
        }

        Object.defineProperty(obj,"name",{
            set(value) {
                name = value;
            },
            get() {
                return name;
            },
            // 可以遍历（默认false）
            enumerable: true,
        })

        console.log(obj);
        

    </script>
</body>
</html>